package com.cloud.kafka;

import com.alibaba.fastjson.JSON;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/kafka")
public class SendController {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    /***
     * 发送消息
     * topic:要发送的队列
     * msg:发送的消息
     */
    @GetMapping(value = "/send/{msg}")
    public String send(@PathVariable(value = "msg") String msg) {
        //消息发送
        kafkaTemplate.send("testTopic", msg);
        return "SUCCESS";
    }

    @GetMapping("/sendDataToKafka")
    public void sendDataToKafka() {
        for (int i = 0; i < 10; i++) {
            KafkaEntity testDemo = new KafkaEntity();
            testDemo.setName("hty" + i);
            testDemo.setNum(i);
            kafkaTemplate.send("testTopic", JSON.toJSONString(testDemo));
        }
    }

    @GetMapping("/RepairTest")
    public void RepairTest() {
        for (int i = 0; i < 10; i++) {
            KafkaEntity testDemo = new KafkaEntity();
            testDemo.setName("hty" + i);
            testDemo.setNum(i);
            kafkaTemplate.send("RepairTest", JSON.toJSONString(testDemo));
        }
    }
}
